import { Controller, Get, Post, Body, HttpException, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { AppI18nService } from '../../i18n/i18n.service';
import { I18nService } from 'nestjs-i18n';
import { CurrentLanguage } from '../../i18n/language.decorator';
import { ApiResponseDto } from '../dto/api-response.dto';

@ApiTags('i18n-test')
@Controller('i18n-test')
export class I18nTestController {
    constructor(
        private readonly i18nService: AppI18nService,
        private readonly nestI18nService: I18nService
    ) { }

    @Get('messages')
    @ApiOperation({
        summary: 'Test i18n messages',
        description: 'Returns various i18n messages for testing'
    })
    @ApiResponse({
        status: 200,
        description: 'I18n messages retrieved successfully'
    })
    async testMessages(@CurrentLanguage() language: string): Promise<ApiResponseDto<any>> {
        const messages = {
            success: this.i18nService.translate('success', {}, language),
            auth: {
                login_success: this.i18nService.getAuthMessage('login_success', {}, language),
                invalid_signature: this.i18nService.getAuthMessage('invalid_signature', {}, language),
            },
            user: {
                not_found: this.i18nService.getUserMessage('not_found', {}, language),
                created: this.i18nService.getUserMessage('created', {}, language),
            },
            kyc: {
                token_generated: this.i18nService.getKycMessage('token_generated', {}, language),
            },
            api: {
                bad_request: this.i18nService.getApiMessage('bad_request', {}, language),
            },
            validation: {
                required: this.i18nService.getValidationMessage('required', {}, language),
                email: this.i18nService.getValidationMessage('email', {}, language),
            }
        };

        const message = this.i18nService.translate('success', {}, language);
        return ApiResponseDto.success(messages, message);
    }

    @Post('error-test')
    @ApiOperation({
        summary: 'Test i18n error handling',
        description: 'Throws various errors to test i18n error handling'
    })
    @ApiResponse({
        status: 400,
        description: 'Bad request error'
    })
    @ApiResponse({
        status: 404,
        description: 'Not found error'
    })
    async testErrors(
        @Body() body: { errorType: string },
        @CurrentLanguage() language: string
    ): Promise<ApiResponseDto<any>> {
        const { errorType } = body;

        switch (errorType) {
            case 'bad_request':
                throw new HttpException(
                    this.i18nService.getApiMessage('bad_request', {}, language),
                    HttpStatus.BAD_REQUEST
                );
            case 'not_found':
                throw new HttpException(
                    this.i18nService.getApiMessage('not_found', {}, language),
                    HttpStatus.NOT_FOUND
                );
            case 'user_not_found':
                throw new HttpException(
                    this.i18nService.getUserMessage('not_found', {}, language),
                    HttpStatus.NOT_FOUND
                );
            case 'invalid_signature':
                throw new HttpException(
                    this.i18nService.getAuthMessage('invalid_signature', {}, language),
                    HttpStatus.UNAUTHORIZED
                );
            default:
                throw new HttpException(
                    this.i18nService.getApiMessage('bad_request', {}, language),
                    HttpStatus.BAD_REQUEST
                );
        }
    }

    @Get('direct-test')
    @ApiOperation({
        summary: 'Test direct i18n service',
        description: 'Test using nestjs-i18n service directly'
    })
    async testDirectI18n(@CurrentLanguage() language: string): Promise<ApiResponseDto<any>> {
        // 直接使用nestjs-i18n服务
        const directTranslation = this.nestI18nService.translate('success', { lang: language });
        const authMessage = this.nestI18nService.translate('auth.login_success', { lang: language });
        const userMessage = this.nestI18nService.translate('user.not_found', { lang: language });

        const messages = {
            directTranslation,
            authMessage,
            userMessage,
            language
        };

        return ApiResponseDto.success(messages, directTranslation as string);
    }
}
